package com.eipc;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends CommonActivity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i = getIntent();
        String url = "";
        if(i!=null){
        	url = i.getStringExtra("url");
        }
        if("".equals(url)){
        	throw new IllegalArgumentException("webview activity must be a url.");
        }
        initView();
        switch(mLocale){
	        case LANGUAGE_CHINESE:
	        	if(url.contains("html-en/")) url = url.replaceFirst("html-en/", "html/");
	        	break;
	        case LANGUAGE_ENGLISH:
	        	if(!url.contains("html-en/")) url = url.replaceFirst("html/", "html-en/");
	        	break;
        }
        mTitle.loadUrl(url);
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(isChild())
				setResult(RESULT_OK);
			else
				finish();
			return true;
		}
		return false;
	}
    
    @Override
    protected void initView() {
    	super.initView();
    	mBar.setMiddleText(R.string.weather);
    	mTitle = (WebView)findViewById(R.id.title);
    	mTitle.setWebViewClient(new WebViewClient(){
    		@Override
    		public void onPageFinished(WebView view, String url) {
    			super.onPageFinished(view, url);
    			mBar.setMiddleText(mTitle.getTitle());
    		}
    	});
    	WebSettings webSettings = mTitle.getSettings();  
    	webSettings.setJavaScriptEnabled(true);  //启动javascript
    	mTitle.addJavascriptInterface(new Object(){
    		public void showImage(String name) { 
    			Intent i = new Intent(WebViewActivity.this, ShowImageActivity.class);
    			i.putExtra("image", name);
    			i.putExtra("screen", "port");
    			startActivity(i);
    		}
    	}, "ipc");
    }
    
    private WebView mTitle;
    private int mName;
   
}